package org.softmed.rest.admin.file;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.softmed.swing.IconManager;

public class ActionPanel2 extends JPanel implements ActionListener {
	JButton createDir = new JButton(IconManager.newFolder);
	JButton createFile = new JButton(IconManager.newDocument);
	JButton openFile = new JButton(IconManager.openDocument);
	JButton refresh = new JButton(IconManager.refresh);

	JButton delete = new JButton(IconManager.remove);

	JButton copy = new JButton(IconManager.copy);
	JButton paste = new JButton(IconManager.paste);

	JButton setRootFolder = new JButton(IconManager.server);

	int width = 30;
	int height = 30;

	FileBrowser fileBrowser;

	public ActionPanel2(FileBrowser fileBrowser) {
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		this.fileBrowser = fileBrowser;
		// setBackground(Color.PINK);

		// setMaximumSize(new Dimension(300, height));
		setMinimumSize(new Dimension(260, height));
		// setPreferredSize(new Dimension(300, height));

		createDir.setToolTipText("Create directory");
		createFile.setToolTipText("Create file");
		openFile.setToolTipText("Open file");
		refresh.setToolTipText("Refresh");
		copy.setToolTipText("Copy");
		paste.setToolTipText("Paste");
		delete.setToolTipText("Delete");
		// setRootFolder.setToolTipText("");

		addButton(createDir);
		addButton(createFile);
		addButton(openFile);
		addButton(refresh);
		addButton(copy);
		addButton(paste);
		addButton(delete);
		addButton(setRootFolder);

		setRootFolder.setToolTipText("Change root folder. Current root: "
				+ fileBrowser.getRootfolder());

		setRootFolder.setEnabled(true);
		refresh.setEnabled(true);

	}

	protected void addButton(JButton button) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
		button.addActionListener(this);
		button.setEnabled(false);
		add(button);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		try {
			Object source = arg0.getSource();

			if (source == setRootFolder) {

				JFileChooser chooser = new JFileChooser();

				chooser.setCurrentDirectory(new File(fileBrowser
						.getRootfolder()));

				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.setDialogTitle("Choose Directory to convert files");

				int returnVal = chooser.showOpenDialog(null);

				if (returnVal != JFileChooser.APPROVE_OPTION)
					return;

				File choice = chooser.getSelectedFile();

				if (!choice.isDirectory())
					return;

				// FileLoader.fileStatus.setDirectory(choice);
				fileBrowser.setRootFolder(choice.getCanonicalPath());
				// setRootFolder.setToolTipText("Change root folder: "
				// + fileBrowser.getRootfolder());
				// loadFilesInDirectory(choice);

				return;
			}

			if (source == delete) {

				int duh = JOptionPane
						.showConfirmDialog(
								this,
								"Are you sure you want to delete this ?\nAll contents will be lost !",
								"Warning", JOptionPane.YES_NO_OPTION,
								JOptionPane.QUESTION_MESSAGE);

				if (duh == JOptionPane.YES_OPTION) {
					fileBrowser.deleteSelection();
				} else {

				}

				return;

			} else if (source == refresh) {
				fileBrowser.refresh();
			} else if (source == createDir) {
				createDirectory();

				// If you're here, the return value was null/empty.
				// setLabel("Come on, finish the sentence!");

			} else if (source == createFile) {
				createFile();

				// If you're here, the return value was null/empty.
				// setLabel("Come on, finish the sentence!");

			}

		} catch (Throwable t) {
			t.printStackTrace();
			JOptionPane.showMessageDialog(this, t.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public String createDirectory() {
		CustomFileNode cnode = fileBrowser.getCurrentSelectedNode();

		String s = (String) JOptionPane.showInputDialog(this,
				"Enter the desired name:", "Input Dialog",
				JOptionPane.PLAIN_MESSAGE, null, null, "");

		// If a string was returned, say so.
		if (s == null || s.length() == 0) {
			// sendErrorPopup("Name Error", "Name can't be empty !");
			return null;
		} else {
			try {
				String fullPath = fileBrowser.createDirectory(cnode
						.getFullPath(), s);
				return fullPath;
			} catch (Throwable t) {
				t.printStackTrace();
				sendFileIOErrorPopup(t.getMessage());
			}
		}
		return null;
	}

	public String createFile() {
		CustomFileNode cnode = fileBrowser.getCurrentSelectedNode();

		String s = (String) JOptionPane.showInputDialog(this,
				"Enter the desired file name:", "Input Dialog",
				JOptionPane.PLAIN_MESSAGE, null, null, "");

		// If a string was returned, say so.
		if (s == null || s.length() == 0) {
			// sendErrorPopup("Name Error", "Name can't be empty !");
			return null;
		} else {
			try {
				String path = cnode.getDirPath();
				if (cnode instanceof DirectoryNode)
					path = cnode.getFullPath();
				String fullPath = fileBrowser.createFile(path, s);
				return fullPath;
			} catch (Throwable t) {
				t.printStackTrace();
				sendFileIOErrorPopup(t.getMessage());
			}
		}
		return null;
	}

	public void enableDirectoryFunctions() {
		setButtonsEnabled(false);
		createDir.setEnabled(true);
		createFile.setEnabled(true);
		// copy.setEnabled(true);
		delete.setEnabled(true);
	}

	public void enableFileFunctions() {
		setButtonsEnabled(false);
		// openFile.setEnabled(true);
		// copy.setEnabled(true);
		delete.setEnabled(true);
	}

	public void setButtonsEnabled(boolean enabled) {
		createDir.setEnabled(enabled);
		createFile.setEnabled(enabled);
		// openFile.setEnabled(enabled);
		// copy.setEnabled(enabled);
		// paste.setEnabled(enabled);
		delete.setEnabled(enabled);
	}

	protected void sendFileIOErrorPopup(String text) {
		sendErrorPopup("File I/O Error", text);

	}

	protected void sendErrorPopup(String title, String text) {
		int duh = JOptionPane.showConfirmDialog(this, text, title,
				JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
	}

	public JButton getSetRootFolder() {
		return setRootFolder;
	}

}